package com.group7.mall.controller;

import com.group7.mall.bean.JsonResult;
import com.group7.mall.bean.Result;
import com.group7.mall.dao.TbGoodsDao;
import com.group7.mall.dao.TbSellerDao;
import com.group7.mall.dao.TbUserDao;
import com.group7.mall.dto.MessageDto;
import com.group7.mall.entity.TbGoods;
import com.group7.mall.entity.TbSeller;
import com.group7.mall.req.MessagePageRequest;
import com.group7.mall.resp.MessagePageResponse;
import com.group7.mall.dto.MessagePreviewDto;
import com.group7.mall.entity.TbMessage;
import com.group7.mall.entity.TbUser;
import com.group7.mall.service.MessageService;
import com.group7.mall.util.Base64Utils;
import com.group7.mall.util.UploadFileToRemoteUtils;
import com.group7.mall.websocket.UserSessionStatus;
import org.reflections.Store;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("message")
public class MessageController {

    @Resource
    private MessageService messageService;

    @Resource
    private TbSellerDao tbSellerDao;

    @Resource
    private TbUserDao tbUserDao;

    @PostMapping("chat-with-seller")
    public Result<TbUser> chatWithSeller(@RequestBody TbGoods tbGoods) {
        return Result.ok(tbUserDao.queryByUserOpenId(tbSellerDao.queryById(tbGoods.getStoreId()).getSellerId()));
    }

    @PostMapping("message-to-read")
    public Result<Integer> messageToRead(@RequestBody TbMessage tbMessage) {
        UserSessionStatus.setLoadingStatus(tbMessage.getFromUserId(), true);
        int i = messageService.messageToRead(tbMessage);
        UserSessionStatus.setLoadingStatus(tbMessage.getFromUserId(), false);
        return Result.ok(i);
    }

    @PostMapping("get-all-unread-message")
    public Result<Integer> getAllUnreadMessage(@RequestBody TbUser tbUser) {
        return Result.ok(messageService.getAllUnreadMessage(tbUser));
    }

    @PostMapping("load-latest-message")
    public Result<MessagePreviewDto> loadLatestMessage(@RequestBody TbUser tbUser) {
        return Result.ok(messageService.queryLatestMessagesByUserId(tbUser.getUserOpenId()));
    }

    @PostMapping("load-messages")
    public Result<MessagePageResponse<TbMessage>> loadMessages(@RequestBody MessagePageRequest messagePageRequest) {
        UserSessionStatus.setLoadingStatus(messagePageRequest.getFromUserId(), true);
        List<TbMessage> tbMessageList = messageService.queryMessageByTwoUser(messagePageRequest);
        UserSessionStatus.setLoadingStatus(messagePageRequest.getFromUserId(), false);
        MessagePageResponse<TbMessage> messageDtoMessagePageResponse = new MessagePageResponse<>();
        messageDtoMessagePageResponse.setRecords(tbMessageList);
        return Result.ok(messageDtoMessagePageResponse);
    }

    @PostMapping("get-history-message")
    public Result<MessagePageResponse<MessageDto>> getHistoryMessage(@RequestBody MessagePageRequest messagePageRequestessagePageRequest) {

        return null;
    }

    @PostMapping("upload-image-message")
    public Result<String> uploadImageMessage(@RequestBody MessageDto messageDto) throws IOException {
        MultipartFile multipartFile = Base64Utils.convertBase64ToMultipart(messageDto.getImageBase64Code(), messageDto.getImageName());
        JsonResult jsonResult = UploadFileToRemoteUtils.uploadFileToRemoteServer(multipartFile, "/opt/docker/apache/www/images/test");
        System.out.println("ok");
        return Result.ok("http://117.72.40.8/images/test/" + messageDto.getImageName());
    }

    @PostMapping("clear-message-in-database")
    public Result<Integer> clearMessageInDatabase() {
        return Result.ok(messageService.clearMessageInDatabase());
    }
}
